<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Weibo_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $openid
     * @return mixed
     * 查询单条
     */
    public function get_weibo_users_by_openid($openid){
        $query = $this->db
            ->select('*')
            ->get_where('weibo_users', array('openid'=>$openid));
        return $query->row_array();
    }

    public function update_weibo_users_by_openid($openid,$data){
        $this->db->where('openid',$openid);
        $this->db->update('weibo_users', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }


    /**
     * @param $id
     * @param $data
     * @return bool
     * 修改微博用户信息
     */
    public function sava_weibo_users($openid,$data){

        $this->db->where('openid',$openid);
        $this->db->update('weibo_users', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    //直接入库
    public function add_first($fromUser){
        $this->db->insert('weibo_users',$fromUser);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $userData
     * @return bool
     * 新增微博用户
     */
    public function subscribe_first($fromUser){

        $user = $this->get_weibo_users_by_openid($fromUser->openid);
        $userData = [
            'openid' => $fromUser->openid,
        ];
        if (!empty($user)) {
            return true;
        } else {
            $this->db->insert('weibo_users',$userData);
        }
        if($this->db->affected_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

    public function weibo_subscribe($fromUser){
        $user = $this->get_weibo_users_by_openid($fromUser['uid']);
        $userData = [
            'nickname' => !empty($fromUser['screen_name']) ? $fromUser['screen_name'] : "0",
            'access_token' => !empty($fromUser['access_token']) ? $fromUser['access_token'] : "0",
            'signup_time' =>date("Y-m-d H:i:s",time()),
            'name'=>$fromUser['name'],
            'image'=>$fromUser['image'],
        ];
        if (!empty($user)) {
            $userData['signin_time'] = date("Y-m-d H:i:s",time());
            $this->sava_weibo_users($user['openid'],$userData);
        } else {
            $userData['openid'] = $fromUser['uid'];
            $this->db->insert('weibo_users',$userData);
            $user = $this->get_weibo_users_by_openid($fromUser['uid']);
        }
        return $user;
    }

    /**
     * @return mixed
     * 查询所有微博用户
     */
    public function get_all_weibo_users(){
        $this->db->select('*');
        $this->db->from('weibo_users');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除微博用户
     */
    public function delete_weibo_users_by($where){
        $this->db->where($where)->delete('weibo_users');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
}
